import { eventBus } from './../common/index'
import { addEventListener } from '../common'
import { EventType } from '../../constants/event'
import { methodEvaler } from '../../core/common'
import { existsSync, mkdirSync } from 'fs-extra'
import { HostType } from '../../constants/common'

const _evalMethod = methodEvaler(HostType.ame)

interface EncodeEvent {
  type: string
  status: string
  data: any
  id: number
  output?: string
}

export enum TaskType {
  PROJECT,
  COMP,
  FILE,
  XML,
}
enum TaskStatus {
  QUEUE,
  PROGRESS,
  FINISH,
  FAILED,
}
export interface TaskInfo {
  source: string
  format?: string
  preset?: string
  sequenceId?: string
  destination?: string
}
export interface Task {
  id: string
  type: TaskType
  info: TaskInfo
  progress: number
  status: TaskStatus
}

// 任务可以考虑本地存储
export const taskMap = new Map<number, Task>()
const handleTaskProgress = ({ data }: { data: EncodeEvent }) => {
  const task = taskMap.get(data.id)
  console.log(data, task)
  eventBus.emit(EventType.EncodeProgress, data)
  if (task) {
    task.progress = data.data
  }
}
const handleTaskFinished = ({ data }: { data: EncodeEvent }) => {
  const task = taskMap.get(data.id)
  console.log(data, task)
  eventBus.emit(EventType.EncodeFinished, data)
  if (task) {
    task.status = 1
    task.progress = 100
  }
}
const handleTaskFailed = ({ data }: { data: EncodeEvent }) => {
  const task = taskMap.get(data.id)
  console.log(data, task)
  eventBus.emit(EventType.EncodeFailed, data)
  if (task) {
    task.status = -1
  }
}
addEventListener(EventType.EncodeProgress, handleTaskProgress)
addEventListener(EventType.EncodeFinished, handleTaskFinished)
addEventListener(EventType.EncodeFailed, handleTaskFailed)

export async function renderProject(
  source: string,
  format: string,
  preset: string,
  sequenceId: string,
  destination: string
) {
  if (!existsSync(source)) {
    throw new Error('Project is not exist')
  }
  if (!existsSync(preset)) {
    throw new Error('Preset is not exist')
  }
  // jiancha shifoukexie
  if (!existsSync(destination)) {
    mkdirSync(destination, { recursive: true })
  }

  const taskId = await _evalMethod(
    'addDLToBatch',
    source,
    format,
    preset,
    sequenceId,
    destination
  )
  if (taskId === 'Bad argument addDLToBatch') {
    throw new Error('参数错误')
  }
  const task: Task = {
    id: taskId,
    type: TaskType.PROJECT,
    status: TaskStatus.QUEUE,
    progress: 0,
    info: {
      source,
      format,
      preset,
      sequenceId,
      destination,
    },
  }
  taskMap.set(parseInt(taskId), task)
  return task
}

export async function renderXML(
  source: string,
  format: string,
  preset: string,
  destination: string
) {
  if (!existsSync(source)) {
    throw new Error('XML is not exist')
  }
  if (!existsSync(preset)) {
    throw new Error('Preset is not exist')
  }
  // jiancha shifoukexie
  if (!existsSync(destination)) {
    mkdirSync(destination, { recursive: true })
  }

  const taskId = await _evalMethod(
    'addXMLToBatch',
    source,
    format,
    preset,
    destination
  )
  if (taskId === 'Bad argument addXMLToBatch') {
    throw new Error('参数错误')
  }
  const task: Task = {
    id: taskId,
    type: TaskType.XML,
    status: TaskStatus.QUEUE,
    progress: 0,
    info: {
      source,
      format,
      preset,
      destination,
    },
  }
  taskMap.set(parseInt(taskId), task)
  return task
}

export async function renderFile(
  source: string,
  format: string,
  preset: string,
  destination: string
) {
  if (!existsSync(source)) {
    throw new Error('File is not exist')
  }
  if (!existsSync(preset)) {
    throw new Error('Preset is not exist')
  }
  // jiancha shifoukexie
  if (!existsSync(destination)) {
    mkdirSync(destination, { recursive: true })
  }

  const taskId = await _evalMethod(
    'addFileToBatch',
    source,
    format,
    preset,
    destination
  )
  if (taskId === 'Bad argument addFileToBatch') {
    throw new Error('参数错误')
  }
  const task: Task = {
    id: taskId,
    type: TaskType.FILE,
    status: TaskStatus.QUEUE,
    progress: 0,
    info: {
      source,
      format,
      preset,
      destination,
    },
  }
  taskMap.set(parseInt(taskId), task)
  return task
}
